home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 December / PCWorld_2004-12_cd.bin / software / temacd / tiny / tf6pro-6[1].0.140.exe / Tiny Firewall Pro 6.0.msi / showlog.js < prev    next >
Encoding:
JavaScript  |  2004-08-27  |  15.8 KB  |  583 lines

  1. /*//////////////////////////////////////////////////////////////////////
  2. filename:         showlogs.js
  3. copyright(c):     Tiny Software corp 2002, 2003 (http://www.tinysoftware.com)
  4. author:         Jozef Palocko (jpalocko@tinysoftware.com)
  5. product:         Tiny Personal Firewall 5.x                
  6. description:     implemetation of Log analyzer
  7. ///////////////////////////////////////////////////////////////////////*/
  8.  
  9. //filter XML file content
  10. var FILTER_XML = '<?xml version="1.0" encoding="UTF-8"?><filter><module/><string/><ar/><app/><fnc/><protocol/><ip/>\
  11.                   <direction/><locport/><remport/><user/><domain/><datetime/><cnt/><sort/></filter>';
  12. var g_lastShownFile = new String();
  13. var    LT_PATH = 1,
  14.     LT_CHECKSUM_AND_PATH = 3;
  15.  
  16. var s_iDisplayType = 0,
  17.     s_arrStyleSheets = new Array("Event_compact.xsl", "Event_detail.xsl"),
  18.     s_arrStyleSheets_ie5 = new Array("Event_compact.xsl", "Event_ie5_detail.xsl");
  19.  
  20. //main funtion
  21. function Main()
  22. {    
  23.     var srcDest = new ActiveXObject("Msxml2.DOMDocument.4.0");
  24.     var nodeRoot = srcDest.selectSingleNode("/");
  25.     var nodeTop = srcDest.createElement("filelist");
  26.     nodeRoot.appendChild(nodeTop);
  27.     srcDest.async=false;
  28.  
  29.     if (!CheckDates())
  30.         return;
  31.                  
  32.     //creates or replaces filter.xml
  33.     InitFilter();               
  34.         
  35.     var ret ="" ;
  36.     idNavBar.innerHTML  ="";
  37.  
  38.     //get dates from form
  39.     var strDate = new String (dateFrom.value);
  40.     dFrom = new Date( Date.parse (strDate.replace ('-',"/")));
  41.       strDate = new String (dateTo.value);
  42.     dTo = new Date(Date.parse (strDate.replace ('-',"/")));
  43.     
  44.     var firstFile ="";
  45.     var strDelimiter = "";
  46.     var strNavBar = "";
  47.  
  48.     var strOneDay ="";
  49.     var strDate ="";
  50.     var fileDayCount = 0;
  51.     var arrLogsByDays = new Array;
  52.  
  53.     arrLogFilesVB  = window.external.GetLogFiles();
  54.     if(arrLogFilesVB!=null)
  55.     {
  56.         arrLogFiles = arrLogFilesVB.toArray();
  57.         for (i = 0; i < arrLogFiles.length; i++)
  58.          {
  59.             dateFile = GetDateOfFile(arrLogFiles[i]);    
  60.             if (dateFile >= dFrom && dateFile <= dTo)
  61.             {
  62.                 //user
  63.                 Node = srcDest.createElement("file");
  64.                 Node.text = arrLogFiles[i];
  65.                 nodeTop.appendChild(Node);
  66.  
  67.                 if (firstFile  == "")
  68.                     firstFile =   GetLogDir() + arrLogFiles[i];
  69.                 var strDate = (dateFile.getMonth()+1) +"."+dateFile.getDate()+"."+dateFile.getYear(); 
  70.                 strDayLog = arrLogsByDays[strDate];
  71.                 if ( strDayLog == null) //create full day string
  72.                 {   
  73.                     fileDayCount =1;
  74.                     strlink = GetHtmlLink(GetLogDir() + arrLogFiles[i], fileDayCount);
  75.                     arrLogsByDays[strDate] = strDate +"  "+strlink+" ";
  76.                 }
  77.                 else //add only new link
  78.                 {     
  79.                      fileDayCount  ++;
  80.                      strlink = ", "+GetHtmlLink(GetLogDir() + arrLogFiles[i], fileDayCount);
  81.                      strDayLog +=  strlink; 
  82.                      arrLogsByDays[strDate]  = strDayLog;
  83.                 }
  84.             }
  85.         }
  86.     }     
  87.                 
  88.     i=0;    
  89.     for (logfile in arrLogsByDays)
  90.     {
  91.         strDelimiter =  i==0? "": " - ";
  92.         strNavBar +=   strDelimiter + arrLogsByDays[logfile] ;    
  93.         i++;
  94.     }        
  95.     
  96.     window.external.SaveTPFFile(srcDest.xml, "loganalyzer/files.xml");
  97.  
  98.     idNavBar.innerHTML = strNavBar;
  99.  
  100.     //set initial file
  101.     if ( firstFile!= "")
  102.     {       
  103.        // document.all.idResultFrame.src = GetLogDir()+ arrLogFiles[0];                   
  104.           ShowLog(firstFile);
  105.     }
  106.  
  107.     OnResize();
  108. }
  109.  
  110. //set url to result frame
  111. function ShowLog(file)
  112. {
  113.     document.frames("idResultFrame").document.body.innerHTML = '<DIV class="clsEvent">Please wait while log is being prepared...</DIV>';
  114.     g_lastShownFile = file;
  115.     setTimeout('ShowLogFinish();', 1);
  116. //    if (IsIE6())
  117. //    {
  118. //       document.all.idResultFrame.src = file;
  119. //    } else
  120. //       document.all.idResult.innerHTML = TransformXML( file, "log.xsl");
  121. }
  122.  
  123. function ShowLogFinish(file)
  124. {
  125.     var strResult;
  126.     if (IsIE6())
  127.         strResult = TransformXML( g_lastShownFile, s_arrStyleSheets[s_iDisplayType]);
  128.     else
  129.         strResult = TransformXML( g_lastShownFile, s_arrStyleSheets_ie5[s_iDisplayType]);
  130.         //strResult = TransformXML( g_lastShownFile, "log-ie5.xsl");
  131.     document.frames("idResultFrame").document.body.innerHTML = strResult;
  132. }
  133. //returns html code for link
  134. function GetHtmlLink(strTarget, strText)
  135. {  
  136.     str  = "<a href=# Name=\""+strTarget+"\" OnClick=\"ShowLog(this.name)\"><b>"+strText+"</b></a>";
  137.     return str;
  138. }
  139. //return date object from given log filename         
  140. function GetDateOfFile(strFile)
  141. {
  142.     arrDate = strFile.split("_");  
  143.     strDate = arrDate[0];
  144.     strYear =  "20" + strDate.substr(0,2);
  145.     strMonth = strDate.substr(2,2);        
  146.     strDay =  strDate.substr(4,2);
  147.     dateFile = new Date(Date.parse(strYear + "/" +strMonth +"/"+strDay));
  148.     return dateFile;
  149. }
  150. //check of existance of file - loads xml file, if file not found return false, else returns true
  151. function CheckFile(filePath)
  152. {
  153.     var bExist = false;
  154.     
  155.     var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
  156.     xmlDoc.async = false;
  157.     xmlDoc.validateOnParse = false;   
  158.     if (xmlDoc.load (filePath))
  159.     {
  160.         xmlDoc = null;
  161.         return true;
  162.     }
  163.     else
  164.     {
  165.         xmlDoc = null;
  166.         return false;
  167.     }
  168. }
  169.  
  170. //check correct date format
  171. function CheckDates()
  172.    var ERR_DATE_FORMAT = "Incorrect date format. Correct format is YYYY/MM/DD.";
  173.    var ERR_DATE_RANGE = "Incorrect date range.";
  174.    var strDate = new String (dateFrom.value);
  175.    dFrom = Date.parse (strDate.replace ('-',"/"));
  176.     
  177.  
  178.     strDate = new String (dateTo.value);
  179.     dTo = Date.parse (strDate.replace ('-',"/"));
  180.  
  181.     
  182.     if (!dFrom  || !dTo)
  183.     {
  184.         alert(ERR_DATE_FORMAT);
  185.         return false;
  186.     }
  187.     if (dFrom>dTo)
  188.     {
  189.         alert(ERR_DATE_RANGE);
  190.         return false;
  191.     }
  192.     return true;
  193. }
  194.  
  195. //returns log filename generated from given date and order:  eg. 030722_001.xml
  196. function GetFileName(d, i)
  197. {
  198.     var strFile = new String;
  199.     var tmp = new String;
  200.     
  201.     tmp += d.getYear();
  202.     strFile += tmp.substr(2,2);
  203.     
  204.     tmp="";    tmp += d.getMonth() + 1;
  205.     if (tmp.length ==1)
  206.         tmp = "0" +tmp;
  207.     strFile +=tmp;
  208.     
  209.     tmp="";tmp += d.getDate();
  210.     if (tmp.length ==1)
  211.         tmp = "0" +tmp;
  212.     strFile += tmp;
  213.     
  214.     var index;
  215.     if (i<10)
  216.         index = "00"+i;
  217.     else if(i<100)
  218.         index = "0"+i;
  219.     else    
  220.        index = i;
  221.        
  222.     strFile += "_"+ index + ".xml";
  223.     return strFile;
  224. }
  225.  
  226.  
  227.  
  228. //returns path of log dir where generated log files are stored
  229. function GetLogDir()
  230. {
  231.     var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
  232.     xmlDoc.async = false;
  233.     xmlDoc.resolveExternals = false;
  234.     xmlDoc.load("logcfg.xml");
  235.     currNode = xmlDoc.selectSingleNode("//Logdir");
  236.     strDir = currNode.text;
  237.     var ch = strDir.charAt(strDir.length-1)
  238.     if (ch !='\\')
  239.         strDir+="\\";
  240.     return strDir;
  241. }
  242.  
  243. //profiler function
  244. function getElapsedTime(date)
  245. {
  246.    d = new Date();                  //Create Date object.
  247.    r = d.getTime() - date.getTime();                //Get current time.
  248.    r = r/1000;
  249.    return(r);                       //Return difference.
  250. }
  251.  
  252.  //initialize date form fields
  253. function InitFields () 
  254. {
  255.     var d = new Date();    
  256.     var strDate = new String;
  257.     var tmp = new String;
  258.     
  259.     tmp += d.getYear();
  260.     strDate += tmp + "/";
  261.     
  262.     tmp="";    tmp += d.getMonth() + 1;
  263.     if (tmp.length ==1)
  264.         tmp = "0" +tmp;
  265.     strDate +=tmp + "/";
  266.     
  267.     tmp="";tmp += d.getDate();
  268.     if (tmp.length ==1)
  269.         tmp = "0" +tmp;
  270.     strDate += tmp;
  271.     
  272.     dateFrom.value = strDate;
  273.     dateTo.value = strDate;
  274.  
  275.     idDivSelectApp.innerHTML = GetComboApps();
  276.    
  277.     var arrOptions = idModule.options, arrSortOptions = idSort.options, i = 0;
  278.     var strPropModule = "";
  279.     strPropModule = window.external.StringProperty("logs-module");
  280.     if (strPropModule != "")
  281.     {
  282.         for (i = 0; i < arrOptions.length; i++)
  283.         {
  284.             if (arrOptions[i].value == strPropModule)
  285.             {
  286.                 idModule.selectedIndex = i;
  287.                 break;
  288.             }
  289.         }
  290.         document.all.idFilter.className = "clsShow";
  291.     }
  292.     var strPropSort = "";
  293.     strPropSort = window.external.StringProperty("logs-sort");
  294.     if (strPropSort != "")
  295.     {
  296.         for (i = 0; i < arrSortOptions.length; i++)
  297.         {
  298.             if (arrSortOptions[i].value == strPropSort)
  299.             {
  300.                 idSort.selectedIndex = i;
  301.                 break;
  302.             }
  303.         }
  304.         document.all.idFilter.className = "clsShow";
  305.     }
  306.     radio_display[s_iDisplayType].checked = 1;
  307. }
  308.  
  309. //loads and saves filter xml file
  310. function InitFilter()
  311. {
  312.     var srcDest = new ActiveXObject("Msxml2.DOMDocument.4.0");
  313.     srcDest.async=false;
  314.     srcDest.loadXML(FILTER_XML); 
  315.  
  316.     //user
  317.     var strMain = window.external.LoggedUser, strUser, strDomain;
  318.     var arrComp = strMain.split("@");
  319.     if (arrComp.length > 1)
  320.     {
  321.         strUser = arrComp[0];
  322.         strDomain = arrComp[1];
  323.     }
  324.     else if (arrComp.length == 1)
  325.     {
  326.         strUser = arrComp[0];
  327.         strDomain = "";
  328.     }
  329.     else
  330.     {
  331.         strUser = "";
  332.         strDomain = "";
  333.     }
  334.     Node = srcDest.selectSingleNode("//user");
  335.     Node.text = strUser
  336.     //domain
  337.     Node = srcDest.selectSingleNode("//domain");
  338.     Node.text = strDomain;
  339.     
  340.     // no. of events
  341.     Node = srcDest.selectSingleNode("//cnt");
  342.     Node.text = select_noevents.options[select_noevents.selectedIndex].value; 
  343.  
  344.     if( document.all.idFilter.className == "clsShow" ) //  BUILD FILTER CONTENT    
  345.     { 
  346.         //set module filter
  347.         Node = srcDest.selectSingleNode("//module");
  348.         Node.text =  idModule.options[idModule.selectedIndex].value;
  349.        //application
  350.         Node = srcDest.selectSingleNode("//app");
  351.         Node.text = idApp.value;
  352.         //acces result
  353.         Node = srcDest.selectSingleNode("//ar");
  354.         Node.text = idAr.options[idAr.selectedIndex].value; 
  355.         //sort order
  356.         Node = srcDest.selectSingleNode("//sort");
  357.         Node.text = idSort.options[idSort.selectedIndex].value;
  358.  
  359.        switch(idModule.options[idModule.selectedIndex].value)
  360.         {
  361.             case "1": //sandbox   
  362.             case "16": //IDS
  363.                 Node = srcDest.selectSingleNode("//string");
  364.                 Node.text = idObject.value;
  365.                 break;
  366.             case "2": //firewall     
  367.                 //protocol
  368.                 Node = srcDest.selectSingleNode("//protocol");
  369.                 Node.text = idProtocol.options[idProtocol.selectedIndex].value;                
  370.                 //ip Adress
  371.                 Node = srcDest.selectSingleNode("//ip");
  372.                 Node.text = idIP.value;
  373.                 //direction
  374.                 Node = srcDest.selectSingleNode("//direction");
  375.                 Node.text = idDirection.options[idDirection.selectedIndex].value;                
  376.                 //loc port
  377.                 Node = srcDest.selectSingleNode("//locport");
  378.                 Node.text = idLocPort.value; 
  379.                 //rem port
  380.                 Node = srcDest.selectSingleNode("//remport");
  381.                 Node.text = idRemPort.value;
  382.                 break;
  383.  
  384.             default:  //All
  385.                 break;       
  386.         }
  387.     }
  388.     //save filter file
  389.     window.external.SaveFilterFile( srcDest.xml);
  390.     
  391.     return srcDest;
  392. }
  393. //return true if is IE version 6 and later, else return false                                           
  394. function IsIE6()
  395. {
  396.     iVersion = parseInt(navigator.appVersion);
  397.     if (-1 != navigator.appVersion.search("MSIE 6"))
  398.         return true;
  399.     else
  400.         return false;
  401. }
  402. //trnsforms XMl file with xsl template
  403. function TransformXML(xmlDoc, xslPath )
  404. {
  405.     var srcTree = new ActiveXObject("Msxml2.DOMDocument.4.0");
  406. // this line caused the currently generated log to not open
  407. //    srcTree.async=false;
  408.     srcTree.load(xmlDoc); 
  409.  
  410.     var xsltTree= new ActiveXObject("Msxml2.DOMDocument.4.0");
  411.     xsltTree.async = false;
  412.     xsltTree.load(xslPath);
  413.  
  414.     return srcTree.transformNode(xsltTree);
  415. }   
  416.  
  417. //handler for showfilter link
  418. function OnShowFilter()
  419. {
  420.    if( document.all.idFilter.className == "clsShow")    
  421.    {
  422.        document.all.idFilter.className = "clsHide";
  423.        idFilterLink.innerText = "Enable Filter ";
  424.    }
  425.    else
  426.    {
  427.        document.all.idFilter.className = "clsShow";
  428.        idFilterLink.innerText = "Disable Filter ";
  429.    }
  430.  
  431.     if (idModule.selectedIndex != 0 ||
  432.         idApp.value != "" ||
  433.         idAr.selectedIndex != 0 ||
  434.         idSort.selectedIndex != 0)
  435.  
  436.        Main();
  437.     
  438.     OnResize();
  439. }                                       
  440.               
  441. // handler for Module combo link
  442. function OnChangeModule(cmbModule)
  443.     switch(cmbModule.options[cmbModule.selectedIndex].value)
  444.     {
  445.         case "2":
  446.             idApp.disabled = false;
  447.             idFWFilter.className="clsShow";
  448.             idSBXFilter.className="clsHide";
  449.             break;
  450.         case "16":
  451.             idApp.value="";
  452.             idSelectApp.selectedIndex = 0;
  453.             idApp.disabled = true;
  454.             idFWFilter.className="clsHide";
  455.             idSBXFilter.className="clsShow";
  456.             break;
  457.         case "1":                       
  458.             idApp.disabled = false;
  459.             idFWFilter.className="clsHide";
  460.             idSBXFilter.className="clsShow";
  461.             break;
  462.         default: 
  463.             idApp.disabled = false;
  464.             idFWFilter.className="clsHide";
  465.             idSBXFilter.className="clsHide";
  466.             break;
  467.     }
  468. }
  469.  
  470. function OnChangeDisplay()
  471. {
  472.     for (i = 0; i < 2; i++) if (radio_display[i].checked==true) 
  473.     {
  474.         s_iDisplayType = radio_display[i].value;
  475.         break;
  476.     }
  477.     Main();
  478.     return 0;
  479. }
  480.  
  481. function OnChangeSort()
  482. {
  483.     if (idSort.selectedIndex == 1)
  484.     {
  485.         idModule.selectedIndex = 0;
  486.         idModule.disabled = 1;
  487.         idApp.value = "";
  488.         idApp.disabled = 1;
  489.         idSelectApp.disabled = 1;
  490.         idAr.selectedIndex = 0;
  491.         idAr.disabled = 1;
  492.     }
  493.     else
  494.     {
  495.         idModule.disabled = 0;
  496.         idApp.disabled = 0;
  497.         idSelectApp.disabled = 0;
  498.         idAr.disabled = 0;
  499.     }
  500.     Main();
  501. }
  502.  
  503. function OnAppSelected()
  504. {
  505.     var strApp = idSelectApp.value;
  506.     if (strApp != "")
  507.         if (strApp == "*")
  508.             idApp.value = "";
  509.         else
  510.             idApp.value = strApp;
  511.     Main();
  512. }
  513.  
  514. function GetComboApps()
  515. {
  516.     var strRet = new String();
  517.     strRet += '<select id="idSelectApp" name="idSelectApp" onchange="OnAppSelected();">';
  518.     strRet += '<option value=""><or choose here></option>';
  519.     strRet += '<option value="*"><All></option>';
  520.     try
  521.     {
  522.         var arrAppParsers = new Array( window.external.ServerParser(64), window.external.ClientParser(64) );
  523.         var pthExpander = new ActiveXObject("SBXPathExpander.SBXPathExpander.1");
  524.         var i = 0;
  525.         for (i = 0; i < arrAppParsers.length; i++)
  526.         {
  527.             var arrLabels = new Array(), arrPaths = new Array();
  528.             var pLabelList = arrAppParsers[i].LabelList;
  529.             var enLabels = new Enumerator(pLabelList);
  530.             enLabels.moveFirst();
  531.             while (!enLabels.atEnd())
  532.             {
  533.                 var pLabel = enLabels.item();
  534.                 if (pLabel.Type == LT_PATH || pLabel.Type == LT_CHECKSUM_AND_PATH)
  535.                 {
  536.                     var arrExp = pthExpander.ExpandFilePath2(pLabel.Path).toArray();
  537.                     if (arrExp.length > 0)
  538.                     {
  539.                         var strLabel = new String(pLabel.LabelID);
  540.                         arrLabels[arrLabels.length] = strLabel;
  541.                         arrPaths[strLabel] = arrExp[0];
  542.                     }
  543.                 }
  544.                 enLabels.moveNext();
  545.             }
  546.             arrLabels.sort();
  547.             var j = 0, n = arrLabels.length;
  548.             for (j = 0; j < n; j++)
  549.             {
  550.                 var strLabel = new String(arrLabels[j]);
  551.                 strRet += '<option value="' + arrPaths[strLabel] + '">' + strLabel + '</option>';
  552.             }
  553.         }
  554.     }
  555.     catch (e)
  556.     {
  557.     }
  558.     strRet += '</select>';
  559.     return strRet;
  560. }
  561.  
  562.  
  563. window.onresize=OnResize;
  564.  
  565.  
  566. function OnResize()
  567. {
  568.     iSize=0;
  569.     if (IsIE6())
  570.     {
  571.          var oParentFrame = window.frameElement;
  572.         iSize=oParentFrame.offsetHeight - document.all.idResult.offsetTop ;
  573.     }
  574.     else
  575.     {
  576.         iSize=document.body.clientHeight  - document.all.idResult.offsetTop - 32 ;
  577.     }
  578.     if(iSize<100) iSize=100;
  579.     document.all.idResult.style.height=iSize;    
  580. }
  581.